//
//  LocationSensor.m
//  UAVClient
//
//  Created by visu4l on 11. 7. 1..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LocationSensor.h"


@implementation LocationSensor

@synthesize latitude;
@synthesize longitude;
@synthesize heading;
@synthesize altitude;
@synthesize speed;

@synthesize locationAcc;
@synthesize altitudeAcc;

-(id)init{
    
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    
    locationManager.headingFilter = kCLHeadingFilterNone;
    [locationManager startUpdatingHeading];
        
    return self;
}

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{    
    double lat = newLocation.coordinate.latitude;
    double lng = newLocation.coordinate.longitude;
    double alt = newLocation.altitude;
    double sp = newLocation.speed;  //m/s
    
    NSLog(@"altitude : %.8f", newLocation.altitude);
    
    
    double locAcc = newLocation.horizontalAccuracy;
    double altAcc = newLocation.verticalAccuracy;
    latitude = (long) (lat * 1000000); //10^6
    longitude = (long) (lng * 1000000); //10^6
    
    altitude =(short) alt;
    speed = (short) sp;
    
    locationAcc = locAcc;
    altitudeAcc = altAcc;
    
}

-(void) locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
    double head = newHeading.magneticHeading; 
    
    heading = (short) head; // range : 0~359
}

-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"Error =========");
    NSLog(@"Code : %i", [error code]);
    NSLog(@"Domain : %@", [error domain]);
    NSLog(@"Reasen : %@", [error localizedDescription]);
    NSLog(@"Solution : %@", [error localizedRecoverySuggestion]);
}

-(void) dealloc{
    [locationManager release];
    [super dealloc];
}

@end
